<!DOCTYPE html>
<html >
<head>

    <!--[if lt IE 9]>
        <style>body {display: none; background: none !important} </style>
        <meta http-equiv="Refresh" Content="0; url=//outdatedbrowser.com/" />
    <![endif]-->

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="format-detection" content="telephone=no" />
<meta name="author" content="Mark" />



<meta name="description" content="作为脚本语言之王的Python的魅力究竟何在。今天，我们就跨入Python学习的历程。去探索Python的魅力。

Python（英国发音：/ˈpaɪθən/ 美国发音：/ˈpaɪθɑːn/）, 是一种面向对象、解释型计算机程序设计语言，由Guido van Rossum于1989年发明，第一个公开发行版发行于1991年。Python是纯粹的自由软件， 源代码和解释器CPython遵循 GPL">
<meta property="og:type" content="article">
<meta property="og:title" content="Python-入门学习（一）">
<meta property="og:url" content="http://markma.tk/posts/Python/Python-入门学习（一）/index.html">
<meta property="og:site_name" content="Mark's Blog">
<meta property="og:description" content="作为脚本语言之王的Python的魅力究竟何在。今天，我们就跨入Python学习的历程。去探索Python的魅力。

Python（英国发音：/ˈpaɪθən/ 美国发音：/ˈpaɪθɑːn/）, 是一种面向对象、解释型计算机程序设计语言，由Guido van Rossum于1989年发明，第一个公开发行版发行于1991年。Python是纯粹的自由软件， 源代码和解释器CPython遵循 GPL">
<meta property="og:image" content="http://i2.hdslb.com/video/ee/ee2aa44bce1a6d7e4603700a0083b97c.jpg">
<meta property="og:updated_time" content="2016-08-25T02:07:38.274Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Python-入门学习（一）">
<meta name="twitter:description" content="作为脚本语言之王的Python的魅力究竟何在。今天，我们就跨入Python学习的历程。去探索Python的魅力。

Python（英国发音：/ˈpaɪθən/ 美国发音：/ˈpaɪθɑːn/）, 是一种面向对象、解释型计算机程序设计语言，由Guido van Rossum于1989年发明，第一个公开发行版发行于1991年。Python是纯粹的自由软件， 源代码和解释器CPython遵循 GPL">
<meta name="twitter:image" content="http://i2.hdslb.com/video/ee/ee2aa44bce1a6d7e4603700a0083b97c.jpg">

<link rel="apple-touch-icon" href= "/apple-touch-icon.png">




    <link rel="shortcut icon" href="/favicon.png">



    <link href="//cdn.bootcss.com/animate.css/3.5.1/animate.min.css" rel="stylesheet">



    <link href="//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.css" rel="stylesheet">



    <script src="//cdn.bootcss.com/pace/1.0.2/pace.min.js"></script>
    <link href="//cdn.bootcss.com/pace/1.0.2/themes/blue/pace-theme-minimal.css" rel="stylesheet">


<link rel="stylesheet" href="/css/style.css">



<link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">


<title>Python-入门学习（一） | Mark&#39;s Blog</title>

<script src="//cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>

<script>
    var yiliaConfig = {
        fancybox: true,
        animate: true,
        isHome: false,
        isPost: true,
        isArchive: false,
        isTag: false,
        isCategory: false,
        fancybox_js: "//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.js",
        scrollreveal: "//cdn.bootcss.com/scrollReveal.js/3.1.4/scrollreveal.min.js",
        search: true
    }
</script>


    <script>
        yiliaConfig.jquery_ui = [true, "//cdn.bootcss.com/jqueryui/1.10.4/jquery-ui.min.js", "//cdn.bootcss.com/jqueryui/1.10.4/css/jquery-ui.min.css"];
    </script>



    <script> yiliaConfig.rootUrl = "\/";</script>





    <script>
        var _hmt = _hmt || [];
        (function() {
            var hm = document.createElement("script");
            hm.src = "//hm.baidu.com/hm.js?fad5c01287c961d95e4fe533d91c8aab";
            var s = document.getElementsByTagName("script")[0]; 
            s.parentNode.insertBefore(hm, s);
        })();
    </script>


</head>
<body>
  <div id="container">
    <div class="left-col">
    <div class="overlay"></div>
<div class="intrude-less">
    <header id="header" class="inner">
        <a href="/" class="profilepic">
            <img src="/img/avatar.png" class="animated zoomIn">
        </a>
        <hgroup>
          <h1 class="header-author"><a href="/">Mark</a></h1>
        </hgroup>

        
        <p class="header-subtitle">To do to make progress</p>
        

        
            <form id="search-form">
            <input type="text" id="local-search-input" name="q" placeholder="search..." class="search form-control" autocomplete="off" autocorrect="off" searchonload="false" />
            <i class="fa fa-times" onclick="resetSearch()"></i>
            </form>
            <div id="local-search-result"></div>
            <p class='no-result'>No results found <i class='fa fa-spinner fa-pulse'></i></p>
        


        
            <div id="switch-btn" class="switch-btn">
                <div class="icon">
                    <div class="icon-ctn">
                        <div class="icon-wrap icon-house" data-idx="0">
                            <div class="birdhouse"></div>
                            <div class="birdhouse_holes"></div>
                        </div>
                        <div class="icon-wrap icon-ribbon hide" data-idx="1">
                            <div class="ribbon"></div>
                        </div>
                        
                        <div class="icon-wrap icon-link hide" data-idx="2">
                            <div class="loopback_l"></div>
                            <div class="loopback_r"></div>
                        </div>
                        
                        
                        <div class="icon-wrap icon-me hide" data-idx="3">
                            <div class="user"></div>
                            <div class="shoulder"></div>
                        </div>
                        
                    </div>
                    
                </div>
                <div class="tips-box hide">
                    <div class="tips-arrow"></div>
                    <ul class="tips-inner">
                        <li>Menu</li>
                        <li>Tags</li>
                        
                        <li>Friends</li>
                        
                        
                        <li>About Me</li>
                        
                    </ul>
                </div>
            </div>
        

        <div id="switch-area" class="switch-area">
            <div class="switch-wrap">
                <section class="switch-part switch-part1">
                    <nav class="header-menu">
                        <ul>
                        
                            <li><a href="/">主页</a></li>
                        
                            <li><a href="/tags/">分类</a></li>
                        
                            <li><a href="/archives/">归档</a></li>
                        
                            <li><a href="/gallery/">相册</a></li>
                        
                            <li><a href="/about/">关于我</a></li>
                        
                        </ul>
                    </nav>
                    <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fa Email" href="mailto:ma-mark@outlook.com" title="Email"></a>
                            
                                <a class="fa 新浪微博" href="http://weibo.com/mark900" title="新浪微博"></a>
                            
                                <a class="fa GitHub" href="https://github.com/markma" title="GitHub"></a>
                            
                                <a class="fa 知乎" href="https://www.zhihu.com/people/mark900" title="知乎"></a>
                            
                                <a class="fa Coding" href="https://coding.net/u/maxiaohang" title="Coding"></a>
                            
                                <a class="fa 网易云音乐" href="http://music.163.com/#/user/home?id=115229883" title="网易云音乐"></a>
                            
                                <a class="fa QQ" href="http://wpa.qq.com/msgrd?v=3&uin=954373276&site=qq&menu=yes" title="QQ"></a>
                            
                        </ul>
                    </nav>
                </section>
                
                
                <section class="switch-part switch-part2">
                    <div class="widget tagcloud" id="js-tagcloud">
                        <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Cpp/">Cpp</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Hexo/">Hexo</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Journal/">Journal</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Matlab/">Matlab</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/OpenLibrary/">OpenLibrary</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Python/">Python</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Qt/">Qt</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Robot/">Robot</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Wiki/">Wiki</a></li></ul>
                    </div>
                </section>
                
                
                
                <section class="switch-part switch-part3">
                    <div id="js-friends">
                    
                      <a class="main-nav-link switch-friends-link" href="http://moxfive.xyz/">MOxFIVE</a>
                    
                    </div>
                </section>
                

                
                
                <section class="switch-part switch-part4">
                
                    <div id="js-aboutme">因为爱好，做着自己本不该做的事。为爱而活，不受羁绊！</div>
                </section>
                
            </div>
        </div>
    </header>                
</div>
    </div>
    <div class="mid-col">
      <nav id="mobile-nav">
      <div class="overlay">
          <div class="slider-trigger"></div>
          <h1 class="header-author js-mobile-header hide"><a href="/" title="回到主页">Mark</a></h1>
      </div>
    <div class="intrude-less">
        <header id="header" class="inner">
            <a href="/" class="profilepic">
                <img src="/img/avatar.png" class="animated zoomIn">
            </a>
            <hgroup>
              <h1 class="header-author"><a href="/" title="回到主页">Mark</a></h1>
            </hgroup>
            
            <p class="header-subtitle">To do to make progress</p>
            
            <nav class="header-menu">
                <ul>
                
                    <li><a href="/">主页</a></li>
                
                    <li><a href="/tags/">分类</a></li>
                
                    <li><a href="/archives/">归档</a></li>
                
                    <li><a href="/gallery/">相册</a></li>
                
                    <li><a href="/about/">关于我</a></li>
                
                <div class="clearfix"></div>
                </ul>
            </nav>
            <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fa Email" target="_blank" href="mailto:ma-mark@outlook.com" title="Email"></a>
                            
                                <a class="fa 新浪微博" target="_blank" href="http://weibo.com/mark900" title="新浪微博"></a>
                            
                                <a class="fa GitHub" target="_blank" href="https://github.com/markma" title="GitHub"></a>
                            
                                <a class="fa 知乎" target="_blank" href="https://www.zhihu.com/people/mark900" title="知乎"></a>
                            
                                <a class="fa Coding" target="_blank" href="https://coding.net/u/maxiaohang" title="Coding"></a>
                            
                                <a class="fa 网易云音乐" target="_blank" href="http://music.163.com/#/user/home?id=115229883" title="网易云音乐"></a>
                            
                                <a class="fa QQ" target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=954373276&site=qq&menu=yes" title="QQ"></a>
                            
                        </ul>
            </nav>
        </header>                
    </div>
    <link class="menu-list" tags="Tags" friends="Friends" about="About Me"/>
</nav>
      <div class="body-wrap"><article id="post-posts/Python/Python-入门学习（一）" class="article article-type-post" itemscope itemprop="blogPost">
  
    <div class="article-meta">
      <a href="/posts/Python/Python-入门学习（一）/" class="article-date">
      <time datetime="2016-08-11T07:42:29.000Z" itemprop="datePublished">2016-08-11</time>
</a>


    </div>
  
  <div class="article-inner">
    
      <input type="hidden" class="isFancy" />
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      Python-入门学习（一）
    </h1>
  

      </header>
      
      <div class="article-info article-info-post">
        
    <div class="article-category tagcloud">
    <a class="article-category-link" href="/categories/Python/">Python</a>
    </div>


        
    <div class="article-tag tagcloud">
        <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Python/">Python</a></li></ul>
    </div>

        <div class="clearfix"></div>
      </div>
      
    
    <div class="article-entry" itemprop="articleBody">
      
          
        <p><img src="http://i2.hdslb.com/video/ee/ee2aa44bce1a6d7e4603700a0083b97c.jpg" width="30%"></p>
<blockquote>
<p>作为脚本语言之王的Python的魅力究竟何在。今天，我们就跨入Python学习的历程。去探索Python的魅力。</p>
<blockquote>
<p>Python（英国发音：/ˈpaɪθən/ 美国发音：/ˈpaɪθɑːn/）, 是一种面向对象、解释型计算机程序设计语言，由Guido van Rossum于1989年发明，第一个公开发行版发行于1991年。<br>Python是纯粹的自由软件， 源代码和解释器CPython遵循 GPL(GNU General Public License)协议[1]  。<br>Python语法简洁清晰，特色之一是强制用空白符(white space)作为语句缩进。<br>Python具有丰富和强大的库。它常被昵称为胶水语言，能够把用其他语言制作的各种模块（尤其是C/C++）很轻松地联结在一起。常见的一种应用情形是，使用Python快速生成程序的原型（有时甚至是程序的最终界面），然后对其中[2]  有特别要求的部分，用更合适的语言改写，比如3D游戏中的图形渲染模块，性能要求特别高，就可以用C/C++重写，而后封装为Python可以调用的扩展类库。需要注意的是在您使用扩展类库时可能需要考虑平台问题，某些可能不提供跨平台的实现。<br><a id="more"></a></p>
</blockquote>
</blockquote>
<h2 id="Python-入门学习-一"><a href="#Python-入门学习-一" class="headerlink" title="Python 入门学习(一)"></a>Python 入门学习(一)</h2><h3 id="Python语言的特色"><a href="#Python语言的特色" class="headerlink" title="Python语言的特色"></a>Python语言的特色</h3><ul>
<li><p>简单<br>Python是一种代表简单主义思想的语言。阅读一个良好的Python程序就感觉像是在读英语一样，尽管这个英语的要求非常严格！Python的这种伪代码本质是它最大的优点之一。它使你能够专注于解决问题而不是去搞明白语言本身。</p>
</li>
<li><p>易学<br>就如同你即将看到的一样，Python极其容易上手。前面已经提到了，Python有极其简单的语法。</p>
</li>
<li><p>免费、开源<br>Python是FLOSS（自由/开放源码软件）之一。简单地说，你可以自由地发布这个软件的拷贝、阅读它的源代码、对它做改动、把它的一部分用于新的自由软件中。FLOSS是基于一个团体分享知识的概念。这是为什么Python如此优秀的原因之一——它是由一群希望看到一个更加优秀的Python的人创造并经常改进着的。</p>
</li>
<li><p>高层语言<br>当你用Python语言编写程序的时候，你无需考虑诸如如何管理你的程序使用的内存一类的底层细节。</p>
</li>
<li><p>可移植性<br>由于它的开源本质，Python已经被移植在许多平台上（经过改动使它能够工作在不同平台上）。如果你小心地避免使用依赖于系统的特性，那么你的所有Python程序无需修改就可以在下述任何平台上面运行。</p>
</li>
</ul>
<blockquote>
<p>这些平台包括Linux、Windows、FreeBSD、Macintosh、Solaris、OS/2、Amiga、AROS、AS/400、BeOS、OS/390、z/OS、Palm OS、QNX、VMS、Psion、Acom RISC OS、VxWorks、PlayStation、Sharp Zaurus、Windows CE甚至还有PocketPC！</p>
</blockquote>
<ul>
<li>解释性<br>这一点需要一些解释。</li>
</ul>
<p>一个用编译性语言比如C或C++写的程序可以从源文件（即C或C++语言）转换到一个你的计算机使用的语言（二进制代码，即0和1）。这个过程通过编译器和不同的标记、选项完成。当你运行你的程序的时候，连接/转载器软件把你的程序从硬盘复制到内存中并且运行。</p>
<p>而Python语言写的程序不需要编译成二进制代码。你可以直接从源代码 运行 程序。在计算机内部，Python解释器把源代码转换成称为字节码的中间形式，然后再把它翻译成计算机使用的机器语言并运行。事实上，由于你不再需要担心如何编译程序，如何确保连接转载正确的库等等，所有这一切使得使用Python更加简单。由于你只需要把你的Python程序拷贝到另外一台计算机上，它就可以工作了，这也使得你的Python程序更加易于移植。</p>
<ul>
<li><p>面向对象<br>Python即支持面向过程的编程也支持面向对象的编程。在 面向过程 的语言中，程序是由过程或仅仅是可重用代码的函数构建起来的。在 面向对象 的语言中，程序是由数据和功能组合而成的对象构建起来的。与其他主要的语言如C++和Java相比，Python以一种非常强大又简单的方式实现面向对象编程。</p>
</li>
<li><p>可扩展性<br>如果你需要你的一段关键代码运行得更快或者希望某些算法不公开，你可以把你的部分程序用C或C++编写，然后在你的Python程序中使用它们。</p>
</li>
<li><p>可嵌入性<br>你可以把Python嵌入你的C/C++程序，从而向你的程序用户提供脚本功能。</p>
</li>
<li><p>丰富的库<br>Python标准库确实很庞大。它可以帮助你处理各种工作，包括正则表达式、文档生成、单元测试、线程、数据库、网页浏览器、CGI、FTP、电子邮件、XML、XML-RPC、HTML、WAV文件、密码系统、GUI（图形用户界面）、Tk和其他与系统有关的操作。记住，只要安装了Python，所有这些功能都是可用的。这被称作Python的“功能齐全”理念。</p>
</li>
<li><p>除了标准库以外，还有许多其他高质量的库，如wxPython、Twisted和Python图像库等等。</p>
</li>
</ul>
<h3 id="初识Python"><a href="#初识Python" class="headerlink" title="初识Python"></a>初识Python</h3><h4 id="Hello-Python"><a href="#Hello-Python" class="headerlink" title="Hello Python !"></a>Hello Python !</h4><p>深入浅出，我们依然从最简单的<em>Hell-world</em>入门。<br><figure class="highlight python"><figcaption><span>Hello Python</span></figcaption><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">print(<span class="string">"Hello Python"</span>)</div></pre></td></tr></table></figure><br>输出:<code>Hello Python</code></p>
<h4 id="Python-语法的不同之处-Tab-键的使用"><a href="#Python-语法的不同之处-Tab-键的使用" class="headerlink" title="Python 语法的不同之处-Tab 键的使用"></a>Python 语法的不同之处-Tab 键的使用</h4><p>有些编程基础的朋友们都知道，在C/C++中程序代码往往会被大括号”{}”所包裹，被称为程序段，那么程序段中的变量，不做特殊说明的话一般为局部变量，其生命周期到括号结束为至。而在Python中，程序段落不再使用{},取而代之的是Tab键的缩进，使得代码更加简洁美观。同时<br>Python中不必使用”;”来代表一句程序的结尾了。</p>
<p>假设 <code>i=2</code><br><figure class="highlight python"><figcaption><span>1</span></figcaption><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">if</span>(i==<span class="number">1</span>)</div><div class="line">print(<span class="string">"1"</span>)</div></pre></td></tr></table></figure><br>输出: <code>1</code> </p>
<figure class="highlight python"><figcaption><span>2</span></figcaption><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">if</span>(i==<span class="number">2</span>)</div><div class="line">	print(<span class="string">"2"</span>)</div></pre></td></tr></table></figure>
<p>输出: <code>2</code><br>可以看到同一缩进列的代码关系平行如兄弟顺序执行，而缩进越靠后的代码辈分越低，也就是程序段越深。</p>
<h4 id="源代码编码"><a href="#源代码编码" class="headerlink" title="源代码编码"></a>源代码编码</h4><p>默认的，Python源文件是以UTF-8编码。在这个编码下，在世界上的大部分语言可以共用字符串，标识符和注释——虽然标准库只使用ASCII字符识别码，这项任何可移植代码都应遵循的公约。为了正确地显示所有的字符，你的编辑器必须能辨认UTF-8编码，而且必须在文件中使用支持所有字符的字体。<br>也可以为源文件指定不同的编码。为了做到这一点，在 #! 行之后添加一个用于限定源文件编码的相当特殊的注释行：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># -*- coding: encoding -*-</span></div></pre></td></tr></table></figure><br>通过此声明，在上例中，源文件的所有东西都将被转换为encoding编码而不是UTF-8编码。</p>
<h3 id="Python概述"><a href="#Python概述" class="headerlink" title="Python概述"></a>Python概述</h3><h4 id="把Python作为一个计算器"><a href="#把Python作为一个计算器" class="headerlink" title="把Python作为一个计算器"></a>把Python作为一个计算器</h4><p>让我们尝试一些简单的Python命令。打开解释器等待主提示符 &gt;&gt;&gt; 的出现（不会花很久）。</p>
<h5 id="数字"><a href="#数字" class="headerlink" title="数字"></a>数字</h5><p>解释器可以作为一个简单的计算器：您可以在解释器里输入一个表达式，它将输出表达式的值。表达式的语法很直白： +, -, * 和 / 和在许多其它语言（如Pascal或C）里一样；括号可以用来为运算分组。例如：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="number">2</span>+<span class="number">2</span></div><div class="line"><span class="number">4</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="comment"># 这是一行注释</span></div><div class="line"><span class="meta">... </span><span class="number">2</span>+<span class="number">2</span></div><div class="line"><span class="number">4</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="number">2</span>+<span class="number">2</span>  <span class="comment"># 这是和代码同在一行的注释</span></div><div class="line"><span class="number">4</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>(<span class="number">50</span><span class="number">-5</span>*<span class="number">6</span>)/<span class="number">4</span></div><div class="line"><span class="number">5.0</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="number">8</span>/<span class="number">5</span> <span class="comment"># 整数除法不会丢失分数部分</span></div><div class="line"><span class="number">1.6</span></div></pre></td></tr></table></figure><br>注意：在不同的机器上浮点运算的结果可能会不一样。之后我们会介绍有关控制浮点运算输出结果的内容。关于浮点数及其表示方式的微妙之处的全面细致的讨论，请参考《浮点运算：问题和限制》。<br>在整数除法中，如果只想得到整数的结果，丢弃可能的分数部分，可以使用运算符 // ：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="comment"># 整数除法返回向下取整后的结果：</span></div><div class="line"><span class="meta">... </span><span class="number">7</span>//<span class="number">3</span></div><div class="line"><span class="number">2</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="number">7</span>//<span class="number">-3</span></div><div class="line"><span class="number">-3</span></div></pre></td></tr></table></figure><br>等号（‘=’）用于给变量赋值。赋值之后，除了下一个提示符，解释器不会显示任何结果。<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span>width = <span class="number">20</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>height = <span class="number">5</span>*<span class="number">9</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>width * height</div><div class="line"><span class="number">900</span></div></pre></td></tr></table></figure><br>一个值可以同时赋予多个变量：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span>x = y = z = <span class="number">0</span>  <span class="comment"># 清零 x, y 和 z</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>x</div><div class="line"><span class="number">0</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>y</div><div class="line"><span class="number">0</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>z</div><div class="line"><span class="number">0</span></div></pre></td></tr></table></figure><br>变量在使用前必须先“定义”（即赋予变量一个值），否则会出现错误：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="comment"># 尝试访问一个未定义的变量</span></div><div class="line"><span class="meta">... </span>n</div><div class="line">Traceback (most recent call last):</div><div class="line">  File <span class="string">"&lt;stdin&gt;"</span>, line <span class="number">1</span>, <span class="keyword">in</span> &lt;module&gt;</div><div class="line">NameError: name <span class="string">'n'</span> <span class="keyword">is</span> <span class="keyword">not</span> defined</div></pre></td></tr></table></figure><br>浮点数得到完全的支持；不同类型的数混合运算时会将整数转换为浮点数：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="number">3</span> * <span class="number">3.75</span> / <span class="number">1.5</span></div><div class="line"><span class="number">7.5</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="number">7.0</span> / <span class="number">2</span></div><div class="line"><span class="number">3.5</span></div></pre></td></tr></table></figure><br>复数也支持；虚数带有 j 或 J 后缀。带有非零实部的复数记为 (实部+虚部j)，或者使用 complex(实部, 虚部) 函数来创建。<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="number">1j</span> * <span class="number">1J</span></div><div class="line">(<span class="number">-1</span>+<span class="number">0j</span>)</div><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="number">1j</span> * complex(<span class="number">0</span>, <span class="number">1</span>)</div><div class="line">(<span class="number">-1</span>+<span class="number">0j</span>)</div><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="number">3</span>+<span class="number">1j</span>*<span class="number">3</span></div><div class="line">(<span class="number">3</span>+<span class="number">3j</span>)</div><div class="line"><span class="meta">&gt;&gt;&gt; </span>(<span class="number">3</span>+<span class="number">1j</span>)*<span class="number">3</span></div><div class="line">(<span class="number">9</span>+<span class="number">3j</span>)</div><div class="line"><span class="meta">&gt;&gt;&gt; </span>(<span class="number">1</span>+<span class="number">2j</span>)/(<span class="number">1</span>+<span class="number">1j</span>)</div><div class="line">(<span class="number">1.5</span>+<span class="number">0.5j</span>)</div></pre></td></tr></table></figure><br>复数总是由两个浮点数来表示，实部和虚部。若要从复数z中提取其中一部分，可使用 z.real 和 z.imag。<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span>a=<span class="number">1.5</span>+<span class="number">0.5j</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>a.real</div><div class="line"><span class="number">1.5</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>a.imag</div><div class="line"><span class="number">0.5</span></div></pre></td></tr></table></figure><br>浮点数和整数之间的转换函数（float(), int()）不能用于复数——尚没办法将复数转换为实数。但可以使用 abs(z) 获得复数的模（结果是浮点数），或者使用 z.real 获取其实部：<br><figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">&gt;&gt;&gt; a=3.0+4.0j</div><div class="line">&gt;&gt;&gt; float(a)</div><div class="line">Traceback (most recent call last):</div><div class="line">  File "&lt;stdin&gt;", line 1, in ?</div><div class="line">TypeError: can't convert complex to float; use abs(z)</div><div class="line">&gt;&gt;&gt; a.real</div><div class="line">3.0</div><div class="line">&gt;&gt;&gt; a.imag</div><div class="line">4.0</div><div class="line">&gt;&gt;&gt; abs(a)  # sqrt(a.real**2 + a.imag**2)</div><div class="line">5.0</div></pre></td></tr></table></figure><br>在交互模式中，最后被输出的表达式结果被赋值给变量 <em> 。这能使您在把Python作为一个桌面计算器使用时使后续计算更方便，例如：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span>tax = <span class="number">12.5</span> / <span class="number">100</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>price = <span class="number">100.50</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>price * tax</div><div class="line"><span class="number">12.5625</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>price + _</div><div class="line"><span class="number">113.0625</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>round(_, <span class="number">2</span>)</div><div class="line"><span class="number">113.06</span></div></pre></td></tr></table></figure><br>此处， </em> 变量就是最后一个计算式的运算结果,应被用户视为只读变量。不要显式地给它赋值——这样您将会创建一个具有相同名称的独立的本地变量，并且屏蔽了这个内置变量的功能。</p>
<h4 id="字符串"><a href="#字符串" class="headerlink" title="字符串"></a>字符串</h4><p>除了数字，Python也能操作字符串。字符串有几种表达方式，可以使用单引号或双引号括起来：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="string">'spam eggs'</span></div><div class="line"><span class="string">'spam eggs'</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="string">'doesn\'t'</span></div><div class="line"><span class="string">"doesn't"</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="string">"doesn't"</span></div><div class="line"><span class="string">"doesn't"</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="string">'"Yes," he said.'</span></div><div class="line"><span class="string">'"Yes," he said.'</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="string">"\"Yes,\" he said."</span></div><div class="line"><span class="string">'"Yes," he said.'</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="string">'"Isn\'t," she said.'</span></div><div class="line"><span class="string">'"Isn\'t," she said.'</span></div></pre></td></tr></table></figure><br>解释器以输入时相同的方式输出字符串操作的结果，即使用引号括起来，并且使用反斜杠转义引号和其它特殊字符来准确地表示。如果字符串包含有单引号但不含双引号，则字符串会用双引号括起来，否则用单引号括起来。对于这样的输入字符串，print() 函数会产生更易读的输出。<br>跨行的字面字符串可用以下几种方法表示。使用续行符，即在每行最后一个字符后使用反斜线来说明下一行是上一行逻辑上的延续：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">hello = <span class="string">"This is a rather long string containing\n\</span></div><div class="line">several lines of text just as you would do in C.\n\</div><div class="line">    Note that whitespace at the beginning of the line is\</div><div class="line"> significant."</div><div class="line"></div><div class="line">print(hello)</div></pre></td></tr></table></figure><br>注意，其中的换行符仍然要使用 \n 表示——反斜杠后的换行符被丢弃了。以上例子将如下输出：<br>    This is a rather long string containing<br>    several lines of text just as you would do in C.<br>        Note that whitespace at the beginning of the line is significant.<br>或者，字符串可以被 “”” （三个双引号）或者 ‘’’ （三个单引号）括起来。使用三引号时，换行符不需要转义，它们会包含在字符串中。以下的例子使用了一个转义符，避免在最开始产生一个不需要的空行。<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">print(<span class="string">"""\</span></div><div class="line">Usage: thingy [OPTIONS]</div><div class="line">     -h                        Display this usage message</div><div class="line">     -H hostname               Hostname to connect to</div><div class="line">""")</div></pre></td></tr></table></figure><br>其输出如下：<br>    Usage: thingy [OPTIONS]<br>         -h                        Display this usage message<br>         -H hostname               Hostname to connect to<br>如果我们使用“原始 (raw)”字符串，那么 \n 不会被转换成换行，行末的的反斜杠，以及源码中的换行符，都将作为数据包含在字符串内。例如：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">hello = <span class="string">r"This is a rather long string containing\n\</span></div><div class="line">several lines of text much as you would do in C."</div><div class="line"></div><div class="line">print(hello)</div></pre></td></tr></table></figure><br>将会输出：<br>    This is a rather long string containing\n\<br>    several lines of text much as you would do in C.<br>字符串可以使用 + 运算符串连接在一起，或者用 * 运算符重复：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span>word = <span class="string">'Help'</span> + <span class="string">'A'</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>word</div><div class="line"><span class="string">'HelpA'</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="string">'&lt;'</span> + word*<span class="number">5</span> + <span class="string">'&gt;'</span></div><div class="line"><span class="string">'&lt;HelpAHelpAHelpAHelpAHelpA&gt;'</span></div></pre></td></tr></table></figure><br>两个紧邻的字面字符串将自动被串连；上例的第一行也可以写成 word = ‘Help’ ‘A’ ；这样的操作只在两个字面值间有效，不能随意用于字符串表达式中：<br><figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">&gt;&gt;&gt; 'str' 'ing'                   #  &lt;-  这样操作正确</div><div class="line">'string'</div><div class="line">&gt;&gt;&gt; 'str'.strip() + 'ing'   #  &lt;-  这样操作正确</div><div class="line">'string'</div><div class="line">&gt;&gt;&gt; 'str'.strip() 'ing'     #  &lt;-  这样操作错误</div><div class="line">  File "&lt;stdin&gt;", line 1, in ?</div><div class="line">    'str'.strip() 'ing'</div><div class="line">                      ^</div><div class="line">SyntaxError: invalid syntax</div></pre></td></tr></table></figure><br>字符串可以被索引；就像 C 语言一样，字符串的第一个字符的索引为 0。没有单独的字符类型；一个字符就是长度为一的字符串。就像Icon编程语言一样，子字符串可以使用分切符来指定：用冒号分隔的两个索引。<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span>word[<span class="number">4</span>]</div><div class="line"><span class="string">'A'</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>word[<span class="number">0</span>:<span class="number">2</span>]</div><div class="line"><span class="string">'Hl'</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>word[<span class="number">2</span>:<span class="number">4</span>]</div><div class="line"><span class="string">'ep'</span></div></pre></td></tr></table></figure><br>默认的分切索引很有用：默认的第一个索引为零，第二个索引默认为字符串可以被分切的长度。<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span>word[:<span class="number">2</span>]    <span class="comment"># 前两个字符</span></div><div class="line"><span class="string">'He'</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>word[<span class="number">2</span>:]    <span class="comment"># 除了前两个字符之外，其后的所有字符</span></div><div class="line"><span class="string">'lpA'</span></div></pre></td></tr></table></figure><br>不同于C字符串的是，Python字符串不能被改变。向一个索引位置赋值会导致错误：<br><figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">&gt;&gt;&gt; word[0] = 'x'</div><div class="line">Traceback (most recent call last):</div><div class="line">  File "&lt;stdin&gt;", line 1, in ?</div><div class="line">TypeError: 'str' object does not support item assignment</div><div class="line">&gt;&gt;&gt; word[:1] = 'Splat'</div><div class="line">Traceback (most recent call last):</div><div class="line">  File "&lt;stdin&gt;", line 1, in ?</div><div class="line">TypeError: 'str' object does not support slice assignment</div></pre></td></tr></table></figure><br>然而，用组合内容的方法来创建新的字符串是简单高效的：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="string">'x'</span> + word[<span class="number">1</span>:]</div><div class="line"><span class="string">'xelpA'</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="string">'Splat'</span> + word[<span class="number">4</span>]</div><div class="line"><span class="string">'SplatA'</span></div></pre></td></tr></table></figure><br>在分切操作字符串时，有一个很有用的规律： s[:i] + s[i:] 等于 s.<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span>word[:<span class="number">2</span>] + word[<span class="number">2</span>:]</div><div class="line"><span class="string">'HelpA'</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>word[:<span class="number">3</span>] + word[<span class="number">3</span>:]</div><div class="line"><span class="string">'HelpA'</span></div></pre></td></tr></table></figure><br>对于有偏差的分切索引的处理方式也很优雅：一个过大的索引将被字符串的大小取代，上限值小于下限值将返回一个空字符串。<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span>word[<span class="number">1</span>:<span class="number">100</span>]</div><div class="line"><span class="string">'elpA'</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>word[<span class="number">10</span>:]</div><div class="line"></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>word[<span class="number">2</span>:<span class="number">1</span>]</div></pre></td></tr></table></figure><br>在索引中可以使用负数，这将会从右往左计数。例如：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span>word[<span class="number">-1</span>]     <span class="comment"># 最后一个字符</span></div><div class="line"><span class="string">'A'</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>word[<span class="number">-2</span>]     <span class="comment"># 倒数第二个字符</span></div><div class="line"><span class="string">'p'</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>word[<span class="number">-2</span>:]    <span class="comment"># 最后两个字符</span></div><div class="line"><span class="string">'pA'</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>word[:<span class="number">-2</span>]    <span class="comment"># 除了最后两个字符之外，其前面的所有字符</span></div><div class="line"><span class="string">'Hel'</span></div></pre></td></tr></table></figure><br>但要注意， -0 和 0 完全一样，所以 -0 不会从右开始计数！<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span>word[<span class="number">-0</span>]     <span class="comment"># (既然 -0 等于 0)</span></div><div class="line"><span class="string">'H'</span></div></pre></td></tr></table></figure><br>超出范围的负数索引会被截去多余部分，但不要尝试在一个单元素索引（非分切索引）里使用：<br><figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">&gt;&gt;&gt; word[-100:]</div><div class="line">'HelpA'</div><div class="line">&gt;&gt;&gt; word[-10]    # 错误</div><div class="line">Traceback (most recent call last):</div><div class="line">  File "&lt;stdin&gt;", line 1, in ?</div><div class="line">IndexError: string index out of range</div></pre></td></tr></table></figure><br>有一个方法可以让您记住分切索引的工作方式，想像索引是指向字符之间，第一个字符左边的数字是 0。接着，有n个字符的字符串最后一个字符的右边是索引n，例如：<br>      +—+—+—+—+—+<br>      | H | e | l | p | A |<br>      +—+—+—+—+—+<br>      0   1   2   3   4   5<br>     -5  -4  -3  -2  -1<br>第一行的数字 0…5 给出了字符串中索引的位置；第二行给出了相应的负数索引。分切部分从 i 到 j 分别由在边缘被标记为 i 和 j 的全部字符组成。<br>对于非负数分切部分，如果索引都在有效范围内，分切部分的长度就是索引的差值。例如， word[1:3] 的长度是2。<br>内置的函数 len() 用于返回一个字符串的长度：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span>s = <span class="string">'supercalifragilisticexpialidocious'</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>len(s)</div><div class="line"><span class="number">34</span></div></pre></td></tr></table></figure><br>关于字符串格式化的信息请参考函数 str.format() 的表述。<br>当字符串和Unicode字符串是%运算符左边的操作数时，请参考旧的格式化操作调用方法。</p>
<h4 id="关于Unicode"><a href="#关于Unicode" class="headerlink" title="关于Unicode"></a>关于Unicode</h4><p>Python从2.0版开始全面支持 Unicode (请参考 <a href="http://www.unicode.org/" target="_blank" rel="external">http://www.unicode.org/</a> )。<br>Unicode为从古至今在脚本中用到的所有字符提供了一个次序列表。起初，只有256个可用的脚本字符次序。文本通常绑定到一个被映射到脚本字符次序的代码页中。这样很容易产生混淆，尤其在国际化的软件方面更是如此（通常记作 i18n —— ‘i’ + 18 个字符 + ‘n’）。Unicode通过为全部脚本定义一个代码页来解决这些问题。<br>如果您在字符串中包含特殊字符，您可以使用 Python Unicode-转义编码。如下例所示：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="string">'Hello\u0020World !'</span></div><div class="line"><span class="string">'Hello World !'</span></div></pre></td></tr></table></figure><br>转义次序 \u0020 用来标志在指定的位置插入次序值是 0x0020 的Unicode字符（空格符）。<br>其它的字符由它们各自的次序值直接解释，就像Unicode次序一样。如果您用到许多西方国家常用的标准拉丁-1编码文本串，你将会发现，较低次序的256个Unicode字符与Latin-1的256个字符一模一样。<br>除了这些标准编码，Python还提供了一整套方法用于在已知编码的基础上创建Unicode字符串。<br>通过使用专为字符串对像提供的 encode() 方法，可以将一个字符串转换为使用特定编码的字节序列，以编码的名称作为参数。首选小写的编码名称。<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="string">"Äpfel"</span>.encode(<span class="string">'utf-8'</span>)</div><div class="line"><span class="string">b'\xc3\x84pfel'</span></div></pre></td></tr></table></figure><br>— 3.1.4. 列表<br>Python囊括了大量的复合数据类型，用于组织其它数值。最有用的是列表，即写在方括号之间、用逗号分隔开的数值列表。列表内的项目不必全是相同的类型。<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span>a = [<span class="string">'spam'</span>, <span class="string">'eggs'</span>, <span class="number">100</span>, <span class="number">1234</span>]</div><div class="line"><span class="meta">&gt;&gt;&gt; </span>a</div><div class="line">[<span class="string">'spam'</span>, <span class="string">'eggs'</span>, <span class="number">100</span>, <span class="number">1234</span>]</div></pre></td></tr></table></figure><br>同字符串索引一样，列表索引也是从 0 开始，列表也可以被分切，被串联，等等：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span>a[<span class="number">0</span>]</div><div class="line"><span class="string">'spam'</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>a[<span class="number">3</span>]</div><div class="line"><span class="number">1234</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>a[<span class="number">-2</span>]</div><div class="line"><span class="number">100</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>a[<span class="number">1</span>:<span class="number">-1</span>]</div><div class="line">[<span class="string">'eggs'</span>, <span class="number">100</span>]</div><div class="line"><span class="meta">&gt;&gt;&gt; </span>a[:<span class="number">2</span>] + [<span class="string">'bacon'</span>, <span class="number">2</span>*<span class="number">2</span>]</div><div class="line">[<span class="string">'spam'</span>, <span class="string">'eggs'</span>, <span class="string">'bacon'</span>, <span class="number">4</span>]</div><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="number">3</span>*a[:<span class="number">3</span>] + [<span class="string">'Boo!'</span>]</div><div class="line">[<span class="string">'spam'</span>, <span class="string">'eggs'</span>, <span class="number">100</span>, <span class="string">'spam'</span>, <span class="string">'eggs'</span>, <span class="number">100</span>, <span class="string">'spam'</span>, <span class="string">'eggs'</span>, <span class="number">100</span>, <span class="string">'Boo!'</span>]</div></pre></td></tr></table></figure><br>所有的分切操作返回一个包含有所需元素的新列表。如下例中，分切将返回列表 a 的一个拷贝：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span>a[:]</div><div class="line">[<span class="string">'spam'</span>, <span class="string">'eggs'</span>, <span class="number">100</span>, <span class="number">1234</span>]</div></pre></td></tr></table></figure><br>与字符串的一成不变有所不同的是，列表可以改变其中的个别元素：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span>a</div><div class="line">[<span class="string">'spam'</span>, <span class="string">'eggs'</span>, <span class="number">100</span>, <span class="number">1234</span>]</div><div class="line"><span class="meta">&gt;&gt;&gt; </span>a[<span class="number">2</span>] = a[<span class="number">2</span>] + <span class="number">23</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>a</div><div class="line">[<span class="string">'spam'</span>, <span class="string">'eggs'</span>, <span class="number">123</span>, <span class="number">1234</span>]</div></pre></td></tr></table></figure><br>通过可以给分切部分赋值，可以改变列表的大小甚至清空列表：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="comment"># 取代一些项目：</span></div><div class="line"><span class="meta">... </span>a[<span class="number">0</span>:<span class="number">2</span>] = [<span class="number">1</span>, <span class="number">12</span>]</div><div class="line"><span class="meta">&gt;&gt;&gt; </span>a</div><div class="line">[<span class="number">1</span>, <span class="number">12</span>, <span class="number">123</span>, <span class="number">1234</span>]</div><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="comment"># 删除一些：</span></div><div class="line"><span class="meta">... </span>a[<span class="number">0</span>:<span class="number">2</span>] = []</div><div class="line"><span class="meta">&gt;&gt;&gt; </span>a</div><div class="line">[<span class="number">123</span>, <span class="number">1234</span>]</div><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="comment"># 插入一些：</span></div><div class="line"><span class="meta">... </span>a[<span class="number">1</span>:<span class="number">1</span>] = [<span class="string">'bletch'</span>, <span class="string">'xyzzy'</span>]</div><div class="line"><span class="meta">&gt;&gt;&gt; </span>a</div><div class="line">[<span class="number">123</span>, <span class="string">'bletch'</span>, <span class="string">'xyzzy'</span>, <span class="number">1234</span>]</div><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="comment"># 在开头插入它本身（一个副本）</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>a[:<span class="number">0</span>] = a</div><div class="line"><span class="meta">&gt;&gt;&gt; </span>a</div><div class="line">[<span class="number">123</span>, <span class="string">'bletch'</span>, <span class="string">'xyzzy'</span>, <span class="number">1234</span>, <span class="number">123</span>, <span class="string">'bletch'</span>, <span class="string">'xyzzy'</span>, <span class="number">1234</span>]</div><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="comment"># 清除列表：把所有项目替换为空列表</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>a[:] = []</div><div class="line"><span class="meta">&gt;&gt;&gt; </span>a</div><div class="line">[]</div></pre></td></tr></table></figure><br>内置函数 len() 也能应用于列表：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span>a = [<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>, <span class="string">'d'</span>]</div><div class="line"><span class="meta">&gt;&gt;&gt; </span>len(a)</div><div class="line"><span class="number">4</span></div></pre></td></tr></table></figure><br>也可以使用嵌套列表（在列表里创建其它列表），例如：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span>q = [<span class="number">2</span>, <span class="number">3</span>]</div><div class="line"><span class="meta">&gt;&gt;&gt; </span>p = [<span class="number">1</span>, q, <span class="number">4</span>]</div><div class="line"><span class="meta">&gt;&gt;&gt; </span>len(p)</div><div class="line"><span class="number">3</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>p[<span class="number">1</span>]</div><div class="line">[<span class="number">2</span>, <span class="number">3</span>]</div><div class="line"><span class="meta">&gt;&gt;&gt; </span>p[<span class="number">1</span>][<span class="number">0</span>]</div><div class="line"><span class="number">2</span></div></pre></td></tr></table></figure><br>您可以在列表的末尾添加项目：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span>p[<span class="number">1</span>].append(<span class="string">'xtra'</span>)</div><div class="line"><span class="meta">&gt;&gt;&gt; </span>p</div><div class="line">[<span class="number">1</span>, [<span class="number">2</span>, <span class="number">3</span>, <span class="string">'xtra'</span>], <span class="number">4</span>]</div><div class="line"><span class="meta">&gt;&gt;&gt; </span>q</div><div class="line">[<span class="number">2</span>, <span class="number">3</span>, <span class="string">'xtra'</span>]</div></pre></td></tr></table></figure><br>注意，在最后一个例子里，p[1] 和 q 实际上指向同一个对象！稍后我们将回到对象语法中继续讨论。</p>
<h4 id="迈出编程第一步"><a href="#迈出编程第一步" class="headerlink" title="迈出编程第一步"></a>迈出编程第一步</h4><p>当然，我们能使用Python完成比 2+2 更复杂的工作。在下例里，我们能写出一个初步的斐波纳契数列如下：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="comment"># Fibonacci series: 斐波纳契数列</span></div><div class="line"><span class="meta">... </span><span class="comment"># 两个元素的总和确定了下一个数</span></div><div class="line"><span class="meta">... </span>a, b = <span class="number">0</span>, <span class="number">1</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">while</span> b &lt; <span class="number">10</span>:</div><div class="line"><span class="meta">... </span>    print(b)</div><div class="line"><span class="meta">... </span>    a, b = b, a+b</div><div class="line">...</div><div class="line"><span class="number">1</span></div><div class="line"><span class="number">1</span></div><div class="line"><span class="number">2</span></div><div class="line"><span class="number">3</span></div><div class="line"><span class="number">5</span></div><div class="line"><span class="number">8</span></div></pre></td></tr></table></figure><br>这个例子介绍了几个新特征。</p>
<ul>
<li>第一行包含了一个复合赋值：变量 a 和 b 同时得到新值 0 和 1。最后一行再次使用了同样的方法，可以看到，右边的表达式会在赋值变动之前执行。右边表达式的执行顺序是从左往右的。</li>
<li>只要条件（这里是： b &lt; 10 ）为真，while循环将一直执行下去。在 Python 里，和C语言中一样，任何非零整数为真；零为假。限定条件可以是一个字符串或是一个列表，事实上可以是任何序列；任何非零长度的值都是真，空序列是假。在例子中用于测试的是一个简单的比较。标准的比较操作写法和C语言一样： &lt; （小于）， &gt; （大于） ， ==  （等于），   &lt;=  （小于等于）  ， &gt;= （大于等于）和 != （不等于）。</li>
<li>书写循环体时需要缩进：缩进是Python组合语句的主要方法。Python（暂）没有提供一个智能输入行的编辑功能，因此您必须在每一个缩进行键入一个制表符或空格符。在实践中您将会乐于使用一个文本编辑器，用以编写更复杂的Python程序；许多文本编辑器有自动缩进功能。当使用解释器交互输入一个复合语句时，语句块必须紧接一个空行表示该语句块结束（因为解释器不会知道您什么时候才会输入最后一行）。注意，同一语句块内的每一行都必须有相同的缩进量。<br>print() 函数输出表达式被赋予的值。它在处理复合表达式、浮点数和字符串方面，不同于仅仅输出的您想写出的表达式（就像我们较早前计算器的例子）。字符串的输出没有引号，各项之间插入空格，所以您可以很好地格式化输出，就像这样：<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span>i = <span class="number">256</span>*<span class="number">256</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span>print(<span class="string">'The value of i is'</span>, i)</div><div class="line">The value of i <span class="keyword">is</span> <span class="number">65536</span></div><div class="line"><span class="number">8</span></div></pre></td></tr></table></figure>
关键字end可以被用于防止输出新的一行，或者在输出的末尾添加不同的字符：<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&gt;&gt;&gt; </span>a, b = <span class="number">0</span>, <span class="number">1</span></div><div class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">while</span> b &lt; <span class="number">1000</span>:</div><div class="line"><span class="meta">... </span>    print(b, end=<span class="string">','</span>)</div><div class="line"><span class="meta">... </span>    a, b = b, a+b</div><div class="line">...</div><div class="line"><span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">5</span>,<span class="number">8</span>,<span class="number">13</span>,<span class="number">21</span>,<span class="number">34</span>,<span class="number">55</span>,<span class="number">89</span>,<span class="number">144</span>,<span class="number">233</span>,<span class="number">377</span>,<span class="number">610</span>,<span class="number">987</span>,</div></pre></td></tr></table></figure>
</li>
</ul>

      
    </div>
    
  </div>
  
    
    <div class="copyright">
        <p><span>Title:</span><a href="/posts/Python/Python-入门学习（一）/">Python-入门学习（一）</a></p>
        <p><span>Author:</span><a href="/" title="Back to Homepage">Mark</a></p>
        <p><span>Created:</span>2016-08-11, 15:42:29</p>
        <p><span>Updated:</span>2016-08-25, 10:07:38</p>
        <p>
            <span>Full URL:</span><a class="post-url" href="/posts/Python/Python-入门学习（一）/" title="Python-入门学习（一）">http://markma.tk/posts/Python/Python-入门学习（一）/</a>
            <span class="copy-path" data-clipboard-text="From http://markma.tk/posts/Python/Python-入门学习（一）/　　By Mark" title="Copy Article&#39;s Link &amp; Author"><i class="fa fa-clipboard"></i></span>
            <script> var clipboard = new Clipboard('.copy-path'); </script>
        </p>
        <p>
            <span>License:</span><i class="fa fa-creative-commons"></i> <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/" title="CC BY-NC-SA 4.0 International" target = "_blank">"CC BY-NC-SA 4.0"</a> Keep Link &amp; Author if Distribute.
        </p>
    </div>



    <nav id="article-nav">
        
            <div id="article-nav-newer" class="article-nav-title">
                <a href="/posts/Python/Python-入门学习（二）/">
                    Python-入门学习（二）
                </a>
            </div>
        
        
            <div id="article-nav-older" class="article-nav-title">
                <a href="/posts/QtStudy/Qt中界面类名的修改/">
                    Qt ui界面调用
                </a>
            </div>
        
    </nav>

  
</article>

    <div id="toc" class="toc-article">
        <strong class="toc-title">Contents</strong>
        
            <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#Python-入门学习-一"><span class="toc-number">1.</span> <span class="toc-text">Python 入门学习(一)</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Python语言的特色"><span class="toc-number">1.1.</span> <span class="toc-text">Python语言的特色</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#初识Python"><span class="toc-number">1.2.</span> <span class="toc-text">初识Python</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#Hello-Python"><span class="toc-number">1.2.1.</span> <span class="toc-text">Hello Python !</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Python-语法的不同之处-Tab-键的使用"><span class="toc-number">1.2.2.</span> <span class="toc-text">Python 语法的不同之处-Tab 键的使用</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#源代码编码"><span class="toc-number">1.2.3.</span> <span class="toc-text">源代码编码</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Python概述"><span class="toc-number">1.3.</span> <span class="toc-text">Python概述</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#把Python作为一个计算器"><span class="toc-number">1.3.1.</span> <span class="toc-text">把Python作为一个计算器</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#数字"><span class="toc-number">1.3.1.1.</span> <span class="toc-text">数字</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#字符串"><span class="toc-number">1.3.2.</span> <span class="toc-text">字符串</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#关于Unicode"><span class="toc-number">1.3.3.</span> <span class="toc-text">关于Unicode</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#迈出编程第一步"><span class="toc-number">1.3.4.</span> <span class="toc-text">迈出编程第一步</span></a></li></ol></li></ol></li></ol>
        
    </div>
    <style>
        .left-col .switch-btn,
        .left-col .switch-area {
            display: none;
        }
        .toc-level-3 i,
        .toc-level-3 ol {
            display: none !important;
        }
    </style>

    <input type="button" id="tocButton" value="Hide"  title="Show or Hide Table of Contents">

    <script>
        yiliaConfig.toc = ["Hide", "Show", !!"false"];
    </script>



    
<div class="share">
    
        <div class="bdsharebuttonbox">
            <a href="#" class="fa fa-twitter bds_twi" data-cmd="twi" title="分享到推特"></a>
            <a href="#" class="fa fa-weibo bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a>
            <a href="#" class="fa fa-qq bds_sqq" data-cmd="sqq" title="分享给 QQ 好友"></a>
            <a href="#" class="fa fa-files-o bds_copy" data-cmd="copy" title="复制网址"></a>
            <a href="#" class="fa fa fa-envelope-o bds_mail" data-cmd="mail" title="通过邮件分享"></a>
            <a href="#" class="fa fa-weixin bds_weixin" data-cmd="weixin" title="生成文章二维码"></a>
            <a href="#" class="fa fa-share-alt bds_more" data-cmd="more"></i></a>
        </div>
        <script>
            window._bd_share_config={
                "common":{"bdSnsKey":{},"bdText":"Python-入门学习（一）　| Mark's Blog　","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"24"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
        </script>
    

    
</div>







    
      <div class="duoshuo" id="comments">
    <div id="comment-box" ></div>
    <div class="ds-thread" id="ds-thread" data-thread-key="posts/Python/Python-入门学习（一）/" data-title="Python-入门学习（一）" data-url="http://markma.tk/posts/Python/Python-入门学习（一）/"></div>
    <script>
        var duoshuoQuery = {short_name:"markma"};
        var loadComment = function(){
            var d = document, s = d.createElement('script');
            s.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
            s.async = true; s.charset = 'UTF-8';
            (d.head || d.body).appendChild(s);
        }

        
    </script>
    
    <script> loadComment(); </script>

</div>
    




    <div class="scroll" id="post-nav-button">
        
            <a href="/posts/Python/Python-入门学习（二）/" title="Pre: Python-入门学习（二）">
                <i class="fa fa-angle-left"></i>
            </a>
        

        <a title="Mini Archives"><i class="fa fa-bars"></i><i class="fa fa-times"></i></a>

        
            <a href="/posts/QtStudy/Qt中界面类名的修改/" title="Next: Qt ui界面调用">
                <i class="fa fa-angle-right"></i>
            </a>
        
    </div>

    <ul class="post-list"><li class="post-list-item"><a class="post-list-link" href="/posts/Matlab/matlab cpp混编/">matlab cpp混编</a></li><li class="post-list-item"><a class="post-list-link" href="/posts/QtStudy/Cpp中回调函数与静态成员/">Cpp中回调函数与静态成员</a></li><li class="post-list-item"><a class="post-list-link" href="/posts/QtStudy/Qt中pro文件写法/">Qt pro文件写法</a></li><li class="post-list-item"><a class="post-list-link" href="/posts/OpenSource/Qt串口开发QextserialPort/">Qt串口开发QextserialPort</a></li><li class="post-list-item"><a class="post-list-link" href="/posts/QtStudy/Qt常见错误汇总/">Qt 常见错误汇总</a></li><li class="post-list-item"><a class="post-list-link" href="/posts/OpenSource/Qt绘图库QCustomplot/">Qt绘图库 QCustomPlot</a></li><li class="post-list-item"><a class="post-list-link" href="/posts/OpenSource/cpp机器人库robot-library/">cpp机器人库robot-library</a></li><li class="post-list-item"><a class="post-list-link" href="/posts/OpenSource/开源库大全/">开源库大全</a></li><li class="post-list-item"><a class="post-list-link" href="/posts/Python/Python-入门学习（二）/">Python-入门学习（二）</a></li><li class="post-list-item"><a class="post-list-link" href="/posts/Python/Python-入门学习（一）/">Python-入门学习（一）</a></li><li class="post-list-item"><a class="post-list-link" href="/posts/QtStudy/Qt中界面类名的修改/">Qt ui界面调用</a></li><li class="post-list-item"><a class="post-list-link" href="/posts/Hexo/HEXO-Github-快速搭建属于自己的博客/">HEXO+Github Create your Blog</a></li><li class="post-list-item"><a class="post-list-link" href="/posts/Journal/hello-world/">Hello World</a></li></ul>




    <script>
        
    </script>
</div>
      <footer id="footer">
    <div class="outer">
        <div id="footer-info">
            <div class="footer-left">
                <i class="fa fa-copyright"></i> 
                2016-2017 Mark
            </div>
            <div class="footer-right">
                <a href="http://hexo.io/" target="_blank" title="A fast, simple &amp; powerful blog framework">Hexo</a>  Theme <a href="https://github.com/MOxFIVE/hexo-theme-yelee" target="_blank" title="Another simple and elegant theme for Hexo  v3.5">Yelee</a> by MOxFIVE <i class="fa fa-heart animated infinite pulse"></i>
            </div>
        </div>
        
            <div class="visit">
                
                    <span id="busuanzi_container_site_pv" style='display:none'>
                        <span id="site-visit" title="Site Visitors"><i class="fa fa-user" aria-hidden="true"></i><span id="busuanzi_value_site_uv"></span>
                        </span>
                    </span>
                
                
                    <span>| </span>
                
                
                    <span id="busuanzi_container_page_pv" style='display:none'>
                        <span id="page-visit"  title="Page Hits"><i class="fa fa-eye animated infinite pulse" aria-hidden="true"></i><span id="busuanzi_value_page_pv"></span>
                        </span>
                    </span>
                
            </div>
        
    </div>
</footer>
    </div>
    
<script data-main="/js/main.js" src="//cdn.bootcss.com/require.js/2.2.0/require.min.js"></script>

    <script>
        $(document).ready(function() {
            var iPad = window.navigator.userAgent.indexOf('iPad');
            if (iPad > -1 || $(".left-col").css("display") === "none") {
                var bgColorList = ["#9db3f4", "#414141", "#e5a859", "#f5dfc6", "#c084a0", "#847e72", "#cd8390", "#996731"];
                var bgColor = Math.ceil(Math.random() * (bgColorList.length - 1));
                $("body").css({"background-color": bgColorList[bgColor], "background-size": "cover"});
            }
            else {
                var backgroundnum = 27;
                var backgroundimg = "url(/background/bg-x.jpg)".replace(/x/gi, Math.ceil(Math.random() * backgroundnum));
                $("body").css({"background": backgroundimg, "background-attachment": "fixed", "background-size": "cover"});
            }
        })
    </script>





    <script type="text/x-mathjax-config">
MathJax.Hub.Config({
    tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
        processEscapes: true,
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    }
});

MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
    for(i=0; i < all.length; i += 1) {
        all[i].SourceElement().parentNode.className += ' has-jax';                 
    }       
});
</script>

<script src="//cdn.bootcss.com/mathjax/2.6.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


<div class="scroll" id="scroll">
    <a href="#" title="Back to Top"><i class="fa fa-arrow-up"></i></a>
    <a href="#comments" onclick="load$hide();" title="Comments"><i class="fa fa-comments-o"></i></a>
    <a href="#footer" title="Go to Bottom"><i class="fa fa-arrow-down"></i></a>
</div>
<script>
    // Open in New Window
    
        var oOpenInNew = {
            
            
            
            
            
            
             archives: ".archive-article-title", 
             miniArchives: "a.post-list-link", 
            
             friends: "#js-friends a", 
             socail: ".social a" 
        }
        for (var x in oOpenInNew) {
            $(oOpenInNew[x]).attr("target", "_blank");
        }
    
</script>

    <script>
        var originTitle = document.title;
        var titleTime;
        document.addEventListener("visibilitychange", function() {
            if (document.hidden) {
                document.title = "(つェ⊂) 我藏好了哦~ " + originTitle;
                clearTimeout(titleTime);
            }
            else {
                document.title = "(*´∇｀*) 被你发现啦~ " + originTitle;
                titleTime = setTimeout(function() {
                    document.title = originTitle;
                }, 2000);
            }
        })
    </script>

<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
</script>
  </div>
</body>
</html>